package com.xxs.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.jfinal.core.JFinal;
import com.xxs.common.Const;
import com.xxs.interceptor.AdminInterceptor;
import com.xxs.utils.Context;
import com.xxs.utils.xml.XmlDom4jUtils;

@Before(AdminInterceptor.class)
@SuppressWarnings("unchecked")
public class CommonController extends BaseAdminController {
    public void index(){
    	sites();
    	resources();
    	render("common.html");
    }
    public void main(){
    	sites();
    	resources();
    	render("main.html");
    }
    public void changeSite(){
    	String siteId = getPara(0);
    	Context.setCurrentSiteId(getSession(), siteId);
    	setAttr("textStatus", "success");
    	renderJson();
    }
    public void refresh(){
    	sites();
		resources();
    	render("/admin/common/common.html");
    }
    
    //循环遍历资源文件
    public void resources(){  
    	Document document = XmlDom4jUtils.getDocument(Const.RESOURCE_PATH);
    	Element element = document.getRootElement();
        List<Element> elements = element.elements();  
        String resources = "[";
        //1.设置内容管理的导航
        List<Element> nodes = XmlDom4jUtils.getElements(getXmlPath(getCurrentSiteCode(), "node"), null);
        for(Element ele : nodes){  
        	String modelInfoId = ele.attributeValue("modelInfoId");
        	if(modelInfoId == null || "".equals(modelInfoId) || "null".equals(modelInfoId)){
        		continue;
        	}
        	resources = resources+"{\"title\":\""+ele.attributeValue("name")+"\",\"icon\":\"fa-cubes\",\"spread\":true,\"href\":\""+JFinal.me().getContextPath()+"/admin/info/list/"+ele.attributeValue("id")+"-"+ele.attributeValue("modelInfoId")+"\",\"children\":[";
        	List<Element> eles = ele.elements();
        	for(Element ele1 : eles){
        		resources = resources+"{\"title\":\""+ele1.attributeValue("name")+"\",\"icon\":\"fa-cubes\",\"href\":\""+JFinal.me().getContextPath()+"/admin/info/list/"+ele1.attributeValue("id")+"-"+ele1.attributeValue("modelInfoId")+"\"},";
        	}
        	resources = resources+"]},";
        }  
        //2.设置resource.xml中配置的导航
        for(Element ele : elements){  
        	resources = resources+"{\"title\":\""+ele.attributeValue("name")+"\",\"icon\":\""+ele.attributeValue("icon")+"\",\"spread\":true,\"children\":[";
        	List<Element> eles = ele.elements();
        	for(Element ele1 : eles){
        		resources = resources+"{\"title\":\""+ele1.attributeValue("name")+"\",\"icon\":\""+ele1.attributeValue("icon")+"\",\"href\":\""+JFinal.me().getContextPath()+ele1.attributeValue("href")+"\"},";
        	}
        	resources = resources+"]},";
        }  
        resources = resources+"]";
//        System.out.println("拼接好的字符串为："+resources);
        setAttr("resources", resources);
        //如果当前节点内容不为空，则输出  
        if(!(element.getTextTrim().equals(""))){  
             System.out.println( element.getName() + "：" + element.getText());    
        }  
        //同时迭代当前节点下面的所有子节点  
        //使用递归  
//        Iterator<Element> iterator = element.elementIterator();  
//        while(iterator.hasNext()){  
//            Element e = iterator.next();  
//            listElements(e);  
//        }  
    }  
    //循环遍历资源文件
    public void sites(){  
    	Document document = XmlDom4jUtils.getDocument(Const.SITE_PATH);
    	Element element = document.getRootElement();
        List<Element> elements = element.elements();  
        Map<String,String> sites = new HashMap<String,String>();
        for(Element ele : elements){  
        	sites.put(ele.attributeValue("id"),ele.attributeValue("name"));
        }  
        setAttr("sites", sites);
    }  
}
